package com.chixing.service.impl;

import com.chixing.mapper.CouponsMapper;
import com.chixing.mapper.MycouponsMapper;
import com.chixing.pojo.CouponMycouponVO;
import com.chixing.pojo.Coupons;
import com.chixing.pojo.Mycoupons;
import com.chixing.pojo.MycouponsExample;
import com.chixing.service.MyCouponService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wtl
 * @Date: 2021/10/17/18:45
 * @Description:
 */
@Service
public class MyCouponServiceImpl implements MyCouponService {
    @Autowired
    private MycouponsMapper mycouponsMapper;
    @Autowired
    private CouponsMapper couponsMapper;


    @Override
    public ServerResponse getMyCoupons(Integer custId) {
        List<CouponMycouponVO> couponMycouponVOS = new ArrayList<>();
        MycouponsExample mycouponsExample = new MycouponsExample();
        mycouponsExample.createCriteria().andCustIdEqualTo(custId).andStatusEqualTo(1);
        List<Mycoupons> mycouponsList = mycouponsMapper.selectByExample(mycouponsExample);
        for (Mycoupons mycoupons:mycouponsList){
            Coupons coupons = couponsMapper.selectByPrimaryKey(mycoupons.getCouponsId());
            couponMycouponVOS.add(new CouponMycouponVO(coupons,mycoupons));
        }
        if (couponMycouponVOS.size()!=0)
            return ServerResponse.getSucccess(couponMycouponVOS);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse save(Mycoupons mycoupons) {
        int rows = mycouponsMapper.insert(mycoupons);
        if (rows>0)
            return ServerResponse.getSucccess(mycoupons);
        return ServerResponse.getFail(null);
    }
}
